package com.sy.process.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.auth.service.SysUserService;
import com.sy.common.result.Result;
import com.sy.model.process.ProcessTemplate;
import com.sy.model.process.ProcessType;
import com.sy.process.service.ProcessService;
import com.sy.process.service.ProcessTemplateService;
import com.sy.process.service.ProcessTypeService;
import com.sy.vo.process.ApprovalVo;
import com.sy.vo.process.ProcessFormVo;
import com.sy.vo.process.ProcessVo;
import com.sy.model.process.Process;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 为员工端提供的接口
 */

@Api(tags = "员工端审批")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin
public class ProcessApiController {

    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ProcessTemplateService processTemplateService;
    @Autowired
    private ProcessService processService;

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        return Result.ok(processService.findStarted(pageParam));
    }

    @ApiOperation(value = "查询已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(@PathVariable Long page, @PathVariable Long limit){
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageModel = processService.findProcessed(pageParam);
        return Result.ok(pageModel);
    }


    @ApiOperation(value = "审批")
    @PostMapping("/approve")
    public Result approve(@RequestBody ApprovalVo approvalVo){
        processService.approve(approvalVo);
        return Result.ok();
    }

    @ApiOperation(value = "查看审批详情信息")
    @GetMapping("/show/{processId}")
    public Result show(@PathVariable Long processId){
        Map<String, Object> map = processService.show(processId);
        return Result.ok(map);
    }

    @ApiOperation(value = "获得待办任务")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(@PathVariable Long page, @PathVariable Long limit){
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageModel = processService.findPending(pageParam);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("/startUp")
    public Result start(@RequestBody ProcessFormVo processFormVo) {
        processService.startUp(processFormVo);
        return Result.ok();
    }

    @ApiOperation(value = "获得审批类型")
    @GetMapping("/findProcessType")
    public Result findProcessType(){
        List<ProcessType> processTypeList = processTypeService.findProcessType();
        return Result.ok(processTypeList);
    }

    @ApiOperation(value = "获得审批模板")
    @GetMapping("/getProcessTemplate/{processTemplateId}")
    public Result findProcessTemplateById(@PathVariable Long processTemplateId){
        ProcessTemplate processTemplate = processTemplateService.getById(processTemplateId);
        return Result.ok(processTemplate);
    }

}
